<template>
  <a-modal
    :title="$t('运营设置')"
    :width="width"
    :visible="visible"
    :zIndex="zIndex"
    :cancelText="$t('关闭')"
    @cancel="handleCancel"
    v-drag
  >
    <template slot="footer">
      <a-button key="back" @click="handleCancel">{{ $t('关闭') }}</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-row>
        <a-col style="color: orange">{{ $t('说明：') }}</a-col>
        <a-col style="color: orange;margin: 5px 30px;">{{ $t('1. 为了您的系统安全，强烈建议您使用安全密码；') }}</a-col>
        <a-col style="color: orange;margin: 5px 30px;">{{ $t('2. 安全密码长度为8~20位，且必须要包括字母、数字和特殊字符；') }}</a-col>
        <a-col style="color: orange;margin: 5px 30px;">{{ $t('3. 若关闭安全密码功能则设置用户登录密码时不再有规则要求，可任意设置。') }}</a-col>
      </a-row>
      <a-row :gutter="[0, 16]" style="padding: 20px 30px 0">
        <a-col :span="24">
          <a-checkbox
            :checked="userSecurityPwd.ivalue == 1"
            @change="(e) => changeSelectState(e, 'userSecurityPwd', 'ivalue', 1, 'autoSave')"
            >{{ $t('安全密码') }}</a-checkbox
          >
        </a-col>
      </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
let that
export default {
  name: 'UserSettingsModal',
  data() {
    return {
      width: 600,
      visible: false,
      zIndex: 1002,
      confirmLoading: false,
      userSecurityPwd: {
        name: 'userSecurityPwd',
        ivalue: 1,
        comment: '用户-安全密码',
      }
    }
  },
  mounted() {
    that = this
  },
  methods: {
    show() {
      that.visible = true
      that.getParameter('userSecurityPwd', 'ivalue', 1)
    },
    getParameter(name, defKey, defValue) {
      getAction('/bis/bisSysParameter/list', { name: name }).then((res) => {
        if (res.success && res.result.records.length > 0) {
          that.$set(that, name, res.result.records[0])
        } else {
          that.$set(that[name], defKey, defValue)
        }
        if(name == "userSecurityPwd"){
          if(that.userSecurityPwd.ivalue == 0) {
            localStorage.setItem('freePass', true)
          } else {
            localStorage.setItem('freePass', false)
          }
        }
      })
    },
    close() {
      that.$emit('close')
      that.visible = false
    },
    handleCancel() {
      that.close()
    },
    changeSelectState(e, name, key, type, arg) {
      if (type == 1) {
        that.$set(that[name], key, e.target.checked ? 1 : 0)
      }
      if (type == 2) {
        that.$set(that[name], key, e.target.checked ? 'Y' : 'N')
      }
      if (arg == 'autoSave') {
        that.parameterChange(name)
      }
    },
    parameterChange(name) {
      let querydata = {}
      let url = '',
        method = ''
      if (that[name].id) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      querydata = Object.assign({}, that[name])
      httpAction(url, querydata, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.getParameter(name)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
  },
}
</script>

<style>
</style>